import { base } from '@/conf'
import Message from '@/util/Message'
import store from '@/store/index'
const myRequest = (method,url,data) => {
	//如果全网址,就不加
	const baseURL =
		/^https:\/\/|http:\/\//.test(url)?
		'':base.baseUrl;
	return new Promise((resolve,reject)=>{
		uni.request({
			url:baseURL + url,
			method:method.toUpperCase(),
			data,//{...} || undefined
      header:{
        'SRC-TOKEN':store.getters.token
      },
			success: result=>{
				if(result.statusCode==200)
					{
						const {code,data,msg} = result.data
						if(code && code!=200){
              Message.error(msg)
              reject(msg)
            }
            resolve(data)
					}
				else{
          Message.error('服务器异常')
					reject(result.toString())
        }
			},
			fail:err=>{
				Message.error('请求异常')
				reject('接口调用失败')
			}
		})
	})
}
export default {
  get:(url,data) => myRequest('get',url,data),
  post:(url,data) => myRequest('post',url,data),
  delete:(url,data) => myRequest('delete',url,data),
  put:(url,data) => myRequest('put',url,data),
}